<?php

namespace app\admin\controller;


use think\Controller;
use app\admin\model\Auth as AuthModel;
use think\Request;
use think\Session;

class Auth extends Controller
{
    // 初始化 防止用户未登录就访问页面
    public function _initialize()
    {
        if (!session('nodeList')) {
            return $this->error('您还未登录,请登录',url('admin/index/index'));
        }
    }
    // 权限列表
    public function index()
    {
        $auth = request()->controller().request()->action();
        $this->judge($auth);

        $auth = new AuthModel();
        $list = $auth->paginate(10);
        return $this->fetch('auth/index', [
            'list' => $list
        ]);
    }

    // 权限添加
    public function create()
    {
        $auth = request()->controller().request()->action();
        $this->judge($auth);

        return $this->fetch('auth/create');
    }

    // 权限添加处理
    public function save(Request $request)
    {

        // 接收数据
        $list = $request->post();
        // halt($list);
        // 模型处理
        $auth = new AuthModel($list);
        // 验证器验证数据
        $check = $this->validate($list, 'admin/auth');
        if (true !== $check) {
            $this->error($check);
            return;
        }
        // 返回受影响行数
        $result = $auth->allowField(true)->save();

        if ($result > 0) {
            return $this->success('添加成功!', 'admin/auth/index');
        } else {
            return $this->error('添加失败,请重试!');
        }
    }

    // 更改页面
    public function edit($id)
    {
        $auth = request()->controller().request()->action();
        $this->judge($auth);

        $auth = AuthModel::get($id);
        return $this->fetch('auth/edit', [
            'list' => $auth
        ]);
    }

    // 更改处理
    public function update(Request $request, $id)
    {

        // halt($request);
        // 接收数据
        $list = $request->put();
        // 模型处理
        $auth = new AuthModel();
        // 验证器验证数据
        $check = $this->validate($list, 'admin/auth');
        if (true !== $check) {
            $this->error($check);
            return;
        }
        // 返回受影响行数
        $result = $auth->allowField(true)->save($list, ['id' => $id]);

        if ($result > 0) {
            return $this->success('更改成功!', 'admin/auth/index');
        } else {
            return $this->error('更改失败,请重试!');
        }
    }

    // 更改状态
    public function delete($id)
    {
        $auth = request()->controller().request()->action();
        $this->judge($auth);

        $auth = AuthModel::get($id);
        $status = $auth->status;
        if ($status == 1) {
            $auth->status = 0;
        } else {
            $auth->status = 1;
        }
        $auth->save();
        return $auth->status;
    }

    /*
     * 判断权限
     */
    public function judge($auth)
    {
        $nodeList = Session::get('nodeList');
        if (empty($nodeList)) {
            return $this->error('您的权限不足!');
        }
        if (!in_array($auth,$nodeList)) {
            return $this->error('您的权限不足!');
        }
    }
}